package com.dwin.h5.app.utils.http;

import android.util.Log;

import com.dwin.h5.app.models.response.BackModel;
import com.dwin.h5.app.models.response.HomeTabRsp;
import com.dwin.h5.app.utils.Constants;

import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 获取主页tab信息
 */
public class UserManageRetrofit {

    private static final boolean isDebugFlag = true;
    public static final String PLATFORM_S = "6";

    private static String getBaseUrl() {
        return isDebugFlag ? Constants.BASE_SYSTEM_URL : Constants.BASE_SYSTEM_URL;
    }

    private static UserManageRetrofit instance = null;

    public static synchronized UserManageRetrofit getInstance() {
        // 这个方法比上面有所改进，不用每次都进行生成对象，只是第一次
        // 使用时生成实例，提高了效率！
        if (instance == null)
            instance = new UserManageRetrofit();
        return instance;
    }

    static Retrofit retrofit;
    static UserManageService userMgrService;

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) { //打印retrofit日志
            Log.e("RetrofitLog", "retrofitBack = " + message);
        }
    });

    private UserManageRetrofit() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()//okhttp设置部分，此处还可再设置网络参数
                .addInterceptor(loggingInterceptor).build();
        retrofit = new Retrofit.Builder().baseUrl(getBaseUrl())
                .client(client)//此client是为了打印信息
                .addConverterFactory(GsonConverterFactory.create()).build();
        userMgrService = retrofit.create(UserManageService.class);
    }

    // 首页获取tab栏信息
    public static void getHomeTabs(String language, Callback<HomeTabRsp> callback) {
        Call<HomeTabRsp> call = userMgrService.getTabList(language, Constants.platform);
        call.enqueue(callback); // 异步发送请求
    }

    // 保存设备的位置信息
    public static void deviceSaveAddress(String auth, RequestBody body, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.deviceSaveAddress(auth, PLATFORM_S, body);
        call.enqueue(callback);
    }

}

